package com.config.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.config.entity.EquStateConfig;
import com.config.service.EquStateConfigService;


@Controller
@RequestMapping("/equipState")
public class EquStateController {
	@Resource(name="equStateConfigService")
	private EquStateConfigService stateService;

	/**
	 * 状态主界面
	 * @return
	 */
	@RequestMapping("/stateMain")
	public String stateMain(Model model){
		model.addAttribute("currentName","设备状态模式配置");
		return "config/equipStateConfig";
	}
	
	@RequestMapping("/findAll")
	public Model findAll(Model model,
			@RequestParam(value = "page", required = false, defaultValue="0")int start,
			@RequestParam(value = "rows", required = false, defaultValue="10")int limit){
		model.addAttribute("rows", stateService.findAll(start, limit));
		model.addAttribute("total", stateService.countByFindAll());
		return model;
	}
	
	@RequestMapping("/save")
	public Model save(Model model,EquStateConfig entity){
		EquStateConfig e=stateService.isRepeat(entity.getStateType());
		if(e!=null){
			model.addAttribute("success",false);
			model.addAttribute("msg","保存失败，不能重复保存相同类型");
		}else{
			stateService.save(entity);
			model.addAttribute("success",true);
			model.addAttribute("msg","保存成功");
		}
		return model;
	}
	
	@RequestMapping("/load")
	public Model load(Model model,int id){
		EquStateConfig entity=stateService.getEquStateConfig(id);
		model.addAttribute("data",entity);
		model.addAttribute("success",true);
		model.addAttribute("msg","加载成功");
		return model;
	}
	
	@RequestMapping("/update")
	public Model update(Model model,EquStateConfig entity){
		stateService.update(entity);
		model.addAttribute("success",true);
		model.addAttribute("msg","修改成功");
		return model;
	}
	
	@RequestMapping("/delete")
	public Model delete(Model model,int id){
		stateService.delete(id);
		model.addAttribute("success",true);
		model.addAttribute("msg","删除成功");
		return model;
	}
}
